package com.jswlw.schedule;

import com.alibaba.fastjson.JSON;
import com.jswlw.entity.biz.ApiFail;
import com.jswlw.entity.biz.DevInfo;
import com.jswlw.entity.biz.DevSet;
import com.jswlw.service.biz.ApiFailService;
import com.jswlw.service.biz.DevInfoService;
import com.jswlw.service.biz.DevSetService;
import com.jswlw.service.helper.CmdType;
import com.jswlw.service.helper.DevApiHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Slf4j
@Component
public class DevSetSchedule {

    @Autowired
    DevInfoService devInfoService;
    @Autowired
    DevSetService devSetService;
    @Autowired
    DevApiHelper devApiHelper;
    @Autowired
    ApiFailService apiFailService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void execute() {
        DevInfo devInfoFilter = new DevInfo();
        devInfoFilter.setBindStatus(1);
        List<DevInfo> list = devInfoService.queryList(devInfoFilter);
        list.forEach(devInfo -> {
            try {
                if (devInfo.getBillTypeId().equals(1) || devInfo.getBillTypeId().equals(2)) {
                    if (devInfo.getRemainTime() >= 0) {
                        ApiFail apiFail = new ApiFail();
                        apiFail.setCmdType(Integer.valueOf(CmdType.SyncTime.getCmd()));
                        apiFail.setIcCid(devInfo.getImei());
                        apiFail.setUrl("");
                        apiFail.setParams("");
                        apiFail.setCreateTime(new Date());
                        apiFail.setFailCount(1);
                        apiFail.setFailContent("");
                        apiFailService.add(apiFail);
                        //devApiHelper.sync_day(devInfo.getImei(), (short) (devInfo.getUsedTime() + 1));
                    }
                }
            } catch (Exception e) {
                log.error("下发定时任务失败", e);
            }

        });
//        DevInfo devInfo=devInfoService.find4Id("166622");
//        if (devInfo.getBillTypeId().equals(1)) {
//            if ((devInfo.getRemainTime() - devInfo.getUsedTime() - 1) > 0) {
//                devApiHelper.sync_day(devInfo.getImei(), (short) (devInfo.getUsedTime() + 1));
//            }
//        }
    }
}
